﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smarly.Forum.Bussines.Model;
using Smarly.Web.Helpers;

namespace Smarly.Web.Areas.StapleForum.Helpers
{
	public static class HtmlHelpers
	{
		private const string urlString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
		private static readonly List<char> urlChars;

		static HtmlHelpers()
		{
			urlChars = urlString.ToCharArray().ToList();
		}

		public static string SectionActionLink(this UrlHelper urlHelper, SectionModel section)
		{
/*
			var urlHelper = html.Url();
			TagBuilder builder = new TagBuilder("a");
			builder.Attributes.Add("href", html.ConvertRelativeUrlToAbsoluteUrl(
				urlHelper.ThreadAction(note) + "#post" + note.Id));
			builder.InnerHtml = "#permanent";
*/
			return urlHelper.Action("Section", "Home", new {section = section.HashTag});
		}

		public static IHtmlString ThreadPermanentActionLink(this HtmlHelper html, ThreadModel.ThreadNote note)
		{
			var urlHelper = html.Url();
			TagBuilder builder = new TagBuilder("a");
			builder.Attributes.Add("href", html.ConvertRelativeUrlToAbsoluteUrl(
				urlHelper.ThreadAction(note) + "#post" + note.Id));
			builder.InnerHtml = "#permanent";
			return new MvcHtmlString(builder.ToString());
		}

		public static string ThreadAction(this UrlHelper urlHelper, ThreadModel.ThreadNote note)
		{
			return urlHelper.Action("Index", "Thread", new {section = note.Thread.SectionHashTag, id = note.Thread.Id, name = note.Thread.Title.StringToUrl()});
		}

		public static string StringToUrl(this string original)
		{
			original = (original ?? string.Empty).ToLower();
			string result = Transliteration.Transliteration.Front(original);
			foreach (char c in result.ToCharArray())
			{
				if (!urlChars.Contains(c))
					result = result.Replace(c, '-');
			}

			return result;
		}

	}
}